//
//  TweetTableViewController.m
//  OSChina
//
//  Created by 8b410-2 on 16/12/25.
//  Copyright © 2016年 abb. All rights reserved.
//

#import "OSCTweetTableViewController.h"
#import "OSCTweetListViewModel.h"
#import "OSCAccountTools.h"
#import "UIView+Blank.h"

static NSString* const reuseTweetCell = @"TweetCell";

@interface OSCTweetTableViewController ()
/// 动弹列表viewModel
@property (nonatomic, strong, readonly) OSCTweetListViewModel *viewModel;

@end

@implementation OSCTweetTableViewController

@dynamic viewModel;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpLoadMore];
    [self setUpRefresh];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    [self.tableView registerClass:[OSCTweetTableViewCell class] forCellReuseIdentifier:reuseTweetCell];
}
- (void)setUpLoadMore {
    if (self.viewModel.type == OSCTweetsTypeHot) {
        return;
    }
    [super setUpLoadMore];
}

/**
 请求
 */
- (void)requestData {
    [self.viewModel.requestListDataCommand execute:nil];
}

#pragma mark - Table view data source

- (UITableViewCell *)tableView:(UITableView *)tableView dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath {
    OSCTweetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseTweetCell forIndexPath:indexPath];
    return cell;
}

- (void)configureCell:(OSCTweetTableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath withObject:(OSCTweetViewModel *)viewModel {
    [cell bindViewModel:viewModel];
}

#pragma mark - Table view delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    OSCTweetViewModel *viewModel = self.viewModel.dataSource[indexPath.row];
    return [tableView fd_heightForCellWithIdentifier:reuseTweetCell cacheByIndexPath:indexPath configuration:^(OSCTweetTableViewCell *cell) {
        [cell bindViewModel:viewModel];
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];

    UIView *selectedCell = [tableView cellForRowAtIndexPath:indexPath];/// 选中的cell实例
    
    CGRect selectedRect = [tableView convertRect:[tableView rectForRowAtIndexPath:indexPath] toView:kKeyWindow];/// 选中的cell相对KeyWindow的rect
    // 打包
    self.selectedTuple = [RACTuple tupleWithObjects:selectedCell,[NSValue valueWithCGRect:selectedRect], nil];
    // 跳转
    OSCTweetViewModel *viewModel = self.viewModel.dataSource[indexPath.row];
    [self.viewModel.didSelectCommand execute:viewModel];
}
@end
